<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-11-27
 * Time: 上午12:30
 */
namespace app\wateradmin\model;

use app\common\model\BaseMember;
use think\Db;

class ShopMember extends BaseMember{

    public function getMemberInfo($condition, $field='*' ){
        if(is_array($condition)){
            $condition['member_store']=session('shop');
        }else{
            $condition.=' AND `member_store`='.session('shop');
        }
        return Db::name('shop_member')->field($field)->where($condition)->find();

    }

    public function getMemberList($condition , $field='*' , $order='member_time DESC' , $page=1 ,$pagesize=15){
        if(is_array($condition)){
            $condition['member_store']=intval(session('shop'));
        }else{
            $condition.=' AND `member_store`='.intval(session('shop'));
        }
        return Db::name('shop_member')->field($field)->where($condition)->order($order)->page($page,$pagesize)->select();

    }

    public function getPageMember($condition , $order='m1.member_time DESC',$page=1 , $pagesize=15){
        if(empty($order)){
            $order='m1.member_login_time DESC,m1.member_time DESC';
        }
        if(!empty($condition)){
            $condition = ' AND '.$condition;
        }
        $limit = ($page-1)*$pagesize;
        $sql = 'SELECT m1.* ,m2.member_name AS parent_name FROM  '.tablename('shop_member').' AS m1 LEFT JOIN '.tablename('shop_member').' AS m2 ON m1.member_parent=m2.member_id WHERE m1.`member_store`='.intval(session('shop')).'  '.$condition.' ORDER BY '.$order.' LIMIT '.$limit.','.$pagesize;
        $result = Db::query($sql);
        foreach($result as $key=>$value){
            $result[$key]['order_count'] = Db::name('shop_order')->where(array('buyer_id'=>$value['member_id']))->count();
            $result[$key]['order_sum'] = Db::name('shop_order')->where(array('buyer_id'=>$value['member_id'],'order_state'=>array('>',10)))->sum('order_amount');
        }
        $member_count = Db::name('shop_member')->alias('m1')->where(' m1.`member_store`='.intval(session('shop')).' '.$condition)->count();
        $pagesion = getPage($member_count,$page,$pagesize);
        return array('list'=>$result,'pagesion'=>$pagesion,'count'=>$member_count);
    }
}